<%@ page contentType="text/html; charset=utf-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="./css/demo.css" type="text/css">
	<link rel="stylesheet" href="./css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="./js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="./js/jquery.ztree.core-3.5.js"></script>

	<!--<script type="text/javascript" src="./js/jquery.ztree.excheck-3.5.js"></script>-->
	<script type="text/javascript" src="./js/jquery.ztree.exedit-3.5.js"></script>
	<SCRIPT type="text/javascript">
		<!--
		
		var setting = {
			edit: {
				enable: true,
				showRenameBtn: false,
				showRemoveBtn : true,
				removeTitle : "删除"
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeDrag: beforeDrag,
				beforeDrop: beforeDrop
			}
		};

		var zNodes = new Array()
		
		function beforeDrag(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return false;
		}
		function beforeDrop(treeId, treeNodes, targetNode, moveType) {
			return targetNode ? targetNode.drop !== false : true;
		}
		
	
		/////////////////////////////////
		
		var setting2 = {
			edit: {
				enable: true,
				showRenameBtn: false,
				showRemoveBtn : false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeDrag: beforeDrag2,
				beforeDrop: beforeDrop2,
				onDrop:onDrop2
			}
		};
		
		
		var zNodes2 = new Array();
		
		function beforeDrag2(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		}
		
		function beforeDrop2(treeId, treeNodes, targetNode, moveType) {
			return targetNode ? targetNode.drop !== false : true;
		}
		
		function onDrop2(){
			zNodes2 = [];
			initRightTreeNodes();
			$.fn.zTree.init($("#treeDemo2"), setting2,zNodes2);	
		}
		

		//全局设置
		jQuery.ajaxSetup({  
		    async : false,
		    contentType: "application/x-www-form-urlencoded; charset=UTF-8"  
		    
		});  
		
		
		$(document).ready(function(){
			if(initLeftTreeNodes() && initRightTreeNodes()){
				$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			  	$.fn.zTree.init($("#treeDemo2"), setting2,zNodes2);
			}
			
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			$.fn.zTree.init($("#treeDemo2"), setting2,zNodes2);
			
			$('#saveRef').live('click', function() {
			  	saveSysRef(); 
			});
			
			var isShowDiv = false;
			
			$("span[id ^='treeDemo'][id $='_span']").live('mouseover', function() {
			  	if(! isShowDiv){
			  		
			  		isShowDiv = true;
			  	}
			});
			
			
			$("span[id ^='treeDemo'][id $='_span']").live('mouseout', function() {
			  	//
			  	
			  	isShowDiv = false;
			});
			
			
		});
		
		
		function initLeftTreeNodes(){
			 var url = '/commonSys/manager/monitor/selectMonSysRef.shtml';
			 var data = {};
			 jQuery.post(url, data, 
				function(rs) {
					if(rs.code != 0) {return false;};
					var array = rs.data;
					if(array.length>0){
						   $.each(array,function(n,value) {  
						   		if(value.PId==0){
						   			zNodes.push( eval('('+ '{id:\"'+value.t_id+'\",r_id:'+value.r_id+',pId:\"'+value.PId+'\",name:\"'+value.server_name+'\('+value.ip+':'+value.port+'\)\['+value.pc_room+'\]\"'+',__type:1,open:true,icon:\"./img/system.png\"}'+')') );
						   		}else{
						   			zNodes.push( eval('('+ '{id:\"'+value.t_id+'\",r_id:'+value.r_id+',pId:\"'+value.PId+'\",name:\"'+value.server_name+'\('+value.ip+':'+value.port+'\)\['+value.pc_room+'\]\"'+',__type:0,open:true,icon:\"./img/server.png\"}'+')') );
						   		}
					       });  
					} else {
					}
				}, 'json'
			);
			
			return true;
		
		}
		
		
		function initRightTreeNodes(){
			 var url = '/commonSys/manager/monitor/selectMonServerByType.shtml';
			 var data = {};
			 jQuery.post(url, data, 
				function(rs) {
					if(rs.code != 0) {return false;};
					var array = rs.data;
					if(array.length>0){
						   $.each(array,function(n,value) {
								zNodes2.push( eval('('+ '{id:\"'+new Date().getTime()+'\",r_id:'+ value.id+',pId:0,name:\"'+value.server_name+'\('+value.ip+':'+value.port+'\)\['+value.pc_room+'\]\"'+',__type:0,open:true,icon:\"./img/server.png\"}'+')') );
					       });  
					} else {
					}
				}, 'json'
			);
			
			return true;
		
		}
		
		
		function saveSysRef(){
			
			var treeObj = $.fn.zTree.getZTreeObj('treeDemo');
			var nodes = treeObj.transformToArray(treeObj.getNodes());
			var r_ids = '',pIds = '',t_ids='';
			var checkTreeNode = '0'; //0:正常；1:服务节点单独作为根节点,2:同一个服务节点，重复存在一个系统中(前提:它们父节点也相同)。
			
			$.each(nodes,function(n,value) {  
				if( value.pId==null && value.__type==1  ){ //根（系统）节点不再保存。
					return true;
				}
				else if( value.pId==null && value.__type==0  ){ //服务节点单独作为根节点 不行。
					checkTreeNode = '1';
					return false;
				}
				/*else if( value.pId==null && value.__type==0  ){ //同一个服务节点，重复存在一个系统中(前提:它们父节点也相同)。
					checkTreeNode = '2';
					return false;
				}*/
				
				r_ids += value.r_id+',';
				t_ids += value.id+',';
				pIds += value.pId+',';
	        });  
	        
	        
	        if(checkTreeNode=='1'){
	        	alert('在系统关系中,不能存在服务节点单独作为根节点。\n请手动删除...');
	        	return;
	        }
	        
	         var url = '/commonSys/manager/monitor/insertMonRef.shtml';
			 var data = {'r_ids':r_ids,'t_ids':t_ids,'pIds':pIds};
			 jQuery.post(url, data, 
				function(rs) {
					if(rs.code != 0) {
						alert('操作失败'); 
						return false;
					}
					alert('操作成功'); 
					window.location.href = '/commonSys/manager/monitor/serverRef.jsp';
				}, 'json'
			);
		}
		
		//-->
	</SCRIPT>
</HEAD>

<BODY>
 <div style="float:left;width:1000px;">
	<h1>巨人平台各系统的关系结构设置</h1>
	<div class="content_wrap">
		<div>
			<ul class="info">
				<li class="title">
					<ul class="list">
						<li>左边div中展示当前各系统的关系结构，右边div中展示了待设置的服务。请把右边的服务节点拖拽至左边树中。</li>
						<li class="highlight_red">注意:在左边的系统关系中,不能存在服务节点单独作为根节点。</li>
					</ul>
				</li>
			</ul>
			<input type="button" value="保存关系"  id="saveRef"  />
		</div>
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree"></ul> 
		</div>
		<div class="right">
			<ul id="treeDemo2" class="ztree"></ul>
		</div>
	</div>
</div>
</BODY>
</HTML>
